package Java_test;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *fail-fast与fail-safe
 *
 * ex = exceptedModCount
 *  m= modCount
 *  m初始为0，经过add与remove后变为6
 *  ex初始是被m赋值的，所以excepted为6
 */

public class modCount_test {
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<>();
        //m++-->1
        arr.add("h");
        //m++-->2
        arr.add("e");
        //m++-->3
        arr.add("l");
        //m++-->4
        arr.add("l");
        //m++-->5
        arr.add("o");
        //m++-->6
        arr.remove(1);
        //exceptedModCount-->6
        Iterator<String> iterator = arr.iterator();

        while (iterator.hasNext()) {
            //ex == 6,,,m == 6
            String str = iterator.next();
            if (str.equals("h")) {
                //ex == 6, m == 7,不相等
                //在下次进入iterator.next()时，
                // 会判断exceptedModCount是否等于modCount
                //如果不相等，则抛出异常
                arr.remove(0);
            }
            System.out.println(str);
        }
    }
}
